<template>
  <div class="shopping-cart">
    <el-drawer
      title="我是标题"
      :visible.sync="drawer"
      :with-header="false"
      :before-close="close"
      :size="400"
    >
      <div class="cart-header">购物车</div>
      <div class="cart-content">
        <div class="cart-item" v-for="(item, index) in cart" :key="index">
          <div class="item-left">
            <img
              src="https://ccdn.goodq.top/caches/2729acc48f69194563f94b87d1ea86c4/aHR0cDovLzViYzZlZDU5MWU3NDMudDczLnFpZmVpeWUuY29tL3FmeS1jb250ZW50L3VwbG9hZHMvMjAxOC8wOS9mZGQ5OTM4ZmE2OWJiZTllMDcxMGFhOTQzN2EzM2JmZi05MHg5MC05MC53ZWJw.webp"
              alt=""
            />
          </div>
          <div class="item-right">
            <div class="name">{{ item.goodsName }}</div>
            <div class="desc">
              <p>{{ item.houseModel }}</p>
              <p>{{ item.serviceTime }}</p>
            </div>
            <div class="price">
              <span>{{ item.num }}</span
              >×<span>¥{{ item.price }}</span>
            </div>
          </div>
          <div class="item-delete" @click="deleteItem(index)">
            <i class="el-icon-close"></i>
          </div>
        </div>
      </div>
      <div class="cart-bottom">
        <div class="sum-price">
          <span>小计¥{{ sumPrice.toFixed(2) }}</span>
        </div>
        <!-- <router-link to="/cart" class="cart-btn btn">
          <el-button>查看购物车</el-button>
        </router-link> -->
        <div class="buy-btn btn" @click="submit">
          <el-button type="primary">结算</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //   drawer: true,
      cart: [],
    };
  },
  props: ["drawer"],
  methods: {
    // 获取本地存储的购物车
    getCartItem() {
      let cart = JSON.parse(localStorage.getItem("cart")) || [];
      this.cart = cart;
    },
    // 删除购物车中商品
    deleteItem(index) {
      const { cart } = this;
      cart.splice(index, 1);
      localStorage.setItem("cart", JSON.stringify(cart));
    },
    // 更改商品数量
    handleChange() {},
    // 关闭购物车
    close() {
      this.$emit("closeCart");
    },
    // 结算
    submit() {
      if (this.cart.length > 0) {
        this.$message({
          message: "结算成功",
          type: "success",
        });
        localStorage.removeItem("cart");
        this.$emit("closeCart");
      } else {
        this.$message({
          message: "购物车内还没有添加服务",
          type: "success",
        });
      }
    },
  },
  computed: {
    sumPrice() {
      const { cart } = this;
      return cart.reduce((acc, item) => {
        return Number(item.price) * item.num + acc;
      }, 0);
    },
  },
};
</script>

<style lang="scss" scoped>
.shopping-cart {
  position: relative;
  /deep/ .el-drawer__body {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .cart-header {
    top: 0;
    width: 400px;
    height: 60px;
    background-color: #000;
    color: #fff;
    font-size: 22px;
    text-align: center;
    line-height: 60px;
  }
  .cart-content {
    display: flex;
    flex-direction: column;
    padding: 10px 50px;
    flex: 1 1;
    overflow: auto;
    .cart-item {
      position: relative;
      display: flex;
      padding: 20px 0;
      border-bottom: 1px solid #ddd;
      .item-right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin-left: 20px;
        font-size: 12px;
        color: #e6454a;
        .desc {
          line-height: 14px;
        }
        .price,
        .name {
          font-size: 14px;
        }
        .name {
          color: #ac1b1b;
        }
      }
      .item-delete {
        position: absolute;
        right: 0;
        top: 20px;
        color: #ddd;
        cursor: pointer;
      }
    }
  }
  .cart-bottom {
    // position: fixed;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 400px;
    padding: 20px;
    height: 200px;
    // bottom: 0;
    box-sizing: border-box;
    .btn {
      width: 270px;
      height: 40px;
      margin-bottom: 10px;
      /deep/ .el-button {
        width: 100%;
        border-radius: 0;
        border: 1px solid #e6454a;
      }
    }
    .cart-btn {
      /deep/ .el-button {
        color: #e6454a;
      }
    }
    .buy-btn {
      /deep/ .el-button {
        background: #e6454a;
      }
    }
    .sum-price {
      margin-bottom: 10px;
    }
  }
}
</style>